var permissons = 1;

var user_id = false;
var sig = false;

var expire = false;
var mid=false;
var secret = false;
var sid = false;

function vk_getUserInfo()
{
    // get name,photo
    get_vk_info(on_get_vk_info);
    // перед тем как нарисовать флешку, спрашиваем у вк инфо
    
}

function get_vk_info(callback)
{
    VK.Api.call('getProfiles',
    {
        uids:user_id,
        fields:"uid, first_name, last_name, nickname, sex, bdate , city, country, photo, photo_medium, photo_big, contacts, education,rate"
    },
    callback);
}

function on_get_vk_info(data)
{
    var uid = data.response[0].uid;
    var first_name = data.response[0].first_name;
    var last_name = data.response[0].last_name;
    var sex = data.response[0].sex;
    var photo = data.response[0].photo;
    var social = {photo:photo,sex:sex,first_name:first_name,last_name:last_name,sex:sex,uid:uid}
    social.social = 1;
    draw_flash(social);
}

function exec(data,callback)
{
     data.user_id = user_id;
     data.sig  = sig;
     data.expire = expire;
     data.mid = mid;
     data.secret = secret;
     data.sid = sid;
     $.ajax({type: 'POST',url: data.url,data:data,dataType:'json',success: callback});
}

function logout_s()
{
	document.location.href = '?logout&'+Math.random();
}

function draw_flash(data,_mid)
{
  if(_mid) mid=_mid;
    //draws flash
    if(mid)
    {
        var flashvars = {
            network_user_id:mid,
            secret:hash,
            first_name:data.first_name,
            last_name:data.last_name,
            sex:data.sex,
            photo:data.photo,
			social:data.social
        };
        swfobject.embedSWF(SWF_URL, "flashContainer", SWF_WIDTH, SWF_HEIGHT, "10.0.0",null, flashvars, {
            allowScriptAccess: "always",
            salign: "t"
        }, {
            id: "flashContainer",
            name: "flashContainer"
        });
        swfobject.createCSS("#flashContainer","outline:none");       
    }
}

function login()
{
    VK.Auth.login(function(response) {

        if (response.session){
            // success
            user_id = response.session['mid'];
            sig = response.session['sig'];
            expire = response.session['expire'];
            mid = response.session['mid'];
            secret = response.session['secret'];
            sid = response.session['sid'];
            document.location.reload();
        } else {
    }
    },permissons);
}

function checkLoginStatus(isLogSer,success_callback)
{
    VK.Auth.getLoginStatus(function(response) {
        if (response.session) {
            user_id = response.session['mid'];
            sig = response.session['sig'];
            expire = response.session['expire'];
            mid = response.session['mid'];
            secret = response.session['secret'];
            sid = response.session['sid'];
            if(!isLogSer) login();
            else if(success_callback)success_callback(1);
        } else {
            if(isLogSer) login();
        }
    },true);
}

function logout()
{
    VK.Auth.logout(function(){
        document.location.reload();
    });

}